package com.zsy._15单例模式;

import java.lang.reflect.Constructor;

/**
 * 双重校验锁 懒汉式
 *
 * @author: zhangshuaiyin
 * @date: 2021/10/4 23:26
 */
public class Single03DCLLazyMan {

    private volatile static Single03DCLLazyMan instance;

    private Single03DCLLazyMan() {
    }

    public static Single03DCLLazyMan getInstance() {
        if (instance == null) {
            synchronized (Single03DCLLazyMan.class) {
                if (instance == null) {
                    instance = new Single03DCLLazyMan();
                }
            }
        }
        return instance;
    }

    // 反射破坏单例
    public static void main(String[] args) throws Exception {
        // LazyMan instance = LazyMan.getInstance();
        Constructor<Single03DCLLazyMan> declaredConstructor =
                Single03DCLLazyMan.class.getDeclaredConstructor(null);
        declaredConstructor.setAccessible(true);
        Single03DCLLazyMan instance = declaredConstructor.newInstance();
        Single03DCLLazyMan instance2 = declaredConstructor.newInstance();
        System.out.println(instance.equals(instance2));
    }
}
